import json

from base.base import Base
from dao.mysqlhelper import MySQLHelper

mysql=MySQLHelper()
# SQL 查询语句
sql = """
SELECT
  	mc.catalog_id,
  	mv.model_vid
FROM
	t_fusion_model_catalog mc
	LEFT JOIN t_fusion_model fm ON mc.catalog_id = fm.catalog_id
	LEFT JOIN t_fusion_model_version mv ON fm.model_id = mv.model_id
	LEFT JOIN t_fusion_model_attribute ma ON mv.model_vid = ma.model_vid 
WHERE
	( mc.namepath LIKE '空间资源%' OR mc.namepath LIKE '感知资源%' OR mc.namepath LIKE '服务资源%' ) 
	AND mc.del_flag = 0
	AND fm.del_flag = 0
--  	AND ma.del_flag != 1
	AND mc.node_level != 1 
GROUP BY
	ma.model_vid 
HAVING
	count(*)=1
"""
re=mysql.select(sql)
print(re)
print(len(re))

base=Base()
if len(re)>0:
    param_type='application/json'
    headers={
        'appcode':'FUSION',
        'authorization':'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIxODM4NTE3NDc2ODU5MTcwODE3Iiwic3ViIjoienpoIiwiYXVkIjoidGVuYW50IiwidW5hbWUiOiJ6emgiLCJhcHBJZCI6Ijk2OWYyYjBjMzY2YTQ2NWE5ZmU4ZGZmMDEyZDE3MjFhIiwiaXNzIjoiaWRhYXMiLCJ0ZW5hbnRJZCI6ImRlZmF1bHRUZW5hbnRJZCIsInV0eXBlIjoidXNlciIsInJuZCI6Imp3bTJzMjJqIiwiZXhwIjoxNzM2OTg3NTYzLCJpYXQiOjE3MzY5MDExNjN9.h6TGhCTAzXaDIqUfZC11bDKl5QeRVOqSvF6ayCCR3Vw'
                    }

    for catalog_id in re:
        data={
            "catalogId": catalog_id[0],
            "modelAttributeList": [
                {
                    "remark": "大地高",
                    "createTime": "2024-12-27 10:31:08",
                    "createBy": "zzh",
                    "updateTime": "2024-12-27 10:31:08",
                    "updateBy": "zzh",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "大地高",
                    "attributeCode": "altitude",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "100",
                    "state": True,
                    "key": "f62e3582bafd4f07a03cbe9bd9502d01"
                },
                {
                    "remark": "行业码",
                    "createTime": "2024-12-27 10:30:44",
                    "createBy": "zzh",
                    "updateTime": "2024-12-27 10:30:44",
                    "updateBy": "zzh",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "行业码",
                    "attributeCode": "biz_code",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "100",
                    "state": True,
                    "key": "a27a708508f04378b44c696fbc91373a"
                },
                {
                    "remark": "时空码",
                    "createTime": "2024-12-27 10:30:30",
                    "createBy": "zzh",
                    "updateTime": "2024-12-27 10:30:30",
                    "updateBy": "zzh",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "时空码",
                    "attributeCode": "geosot_code",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "100",
                    "state": True,
                    "key": "1c917bdf55d1450fa7b8e213e42358a0"
                },
                {
                    "remark": "归属单位",
                    "createTime": "2024-12-27 10:30:14",
                    "createBy": "zzh",
                    "updateTime": "2024-12-27 10:30:14",
                    "updateBy": "zzh",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "归属单位",
                    "attributeCode": "belong_unit",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "100",
                    "state": True,
                    "key": "36badc9400eb4bd881366fc7986bddff"
                },
                {
                    "remark": "管理单位",
                    "createTime": "2024-12-27 10:29:51",
                    "createBy": "zzh",
                    "updateTime": "2024-12-27 10:29:51",
                    "updateBy": "zzh",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "管理单位",
                    "attributeCode": "manage_unit",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "100",
                    "state": True,
                    "key": "61ee7212a2f44d7b98647dc7a42ddda5"
                },
                {
                    "remark": "实体分类",
                    "createTime": "2024-12-27 10:29:15",
                    "createBy": "zzh",
                    "updateTime": "2024-12-27 10:29:26",
                    "updateBy": "zzh",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "分类",
                    "attributeCode": "category_id",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "100",
                    "state": True,
                    "key": "87333c904535407d921f8c1b22586929"
                },
                {
                    "remark": "纬度",
                    "createTime": "2024-12-26 15:32:09",
                    "createBy": "yusidu",
                    "updateTime": "2024-12-26 15:32:09",
                    "updateBy": "yusidu",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "纬度",
                    "attributeCode": "latitude",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "64",
                    "state": True,
                    "key": "982289520c794bf08f531863683823fc"
                },
                {
                    "remark": "经度",
                    "createTime": "2024-12-26 15:31:50",
                    "createBy": "yusidu",
                    "updateTime": "2024-12-26 15:31:50",
                    "updateBy": "yusidu",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "经度",
                    "attributeCode": "longitude",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "64",
                    "state": True,
                    "key": "a7002bb89a854471bd2f62d7b8832c63"
                },
                {
                    "remark": "归属空间统一码",
                    "createTime": "2024-12-26 14:58:55",
                    "createBy": "yusidu",
                    "updateTime": "2024-12-26 14:58:55",
                    "updateBy": "yusidu",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "归属空间统一码",
                    "attributeCode": "GSKJ_TYM",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "64",
                    "state": True,
                    "key": "108168143a4c41d6bc0ad828e3a1271b"
                },
                {
                    "remark": "归属空间名称",
                    "createTime": "2024-12-26 14:57:04",
                    "createBy": "yusidu",
                    "updateTime": "2024-12-26 14:57:04",
                    "updateBy": "yusidu",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "归属空间名称",
                    "attributeCode": "GSKJ_NAME",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "1000",
                    "state": True,
                    "key": "1b62198b2c3c46e690bf159cf8a9ae55"
                },
                {
                    "remark": "名称",
                    "createTime": "2024-12-26 14:34:55",
                    "createBy": "yusidu",
                    "updateTime": "2024-12-26 14:34:55",
                    "updateBy": "yusidu",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "名称",
                    "attributeCode": "ALL_ADRR_NAME",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "256",
                    "state": True,
                    "key": "7aa6a7d8f9834a4f815682e797929169"
                },
                {
                    "remark": "统一码",
                    "createTime": "2024-12-26 10:33:43",
                    "createBy": "yusidu",
                    "updateTime": "2024-12-26 12:57:32",
                    "updateBy": "admin",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "统一码",
                    "attributeCode": "ALL_TYM",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "32",
                    "state": True,
                    "key": "e46a1a40b98940ab8ddf8e47403e69ac"
                },
                {
                    "remark": "地址",
                    "createTime": "2024-12-26 10:41:02",
                    "createBy": "yusidu",
                    "updateTime": "2024-12-26 10:41:02",
                    "updateBy": "yusidu",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "地址",
                    "attributeCode": "ALL_ADDRESS",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "1000",
                    "state": True,
                    "key": "5ca8bf98561c44a99c88c9a51e3ef54b"
                },
                {
                    "remark": "归属单位名称",
                    "createTime": "2025-02-07 16:54:13",
                    "createBy": "zzh",
                    "updateTime": "2025-02-07 16:54:13",
                    "updateBy": "zzh",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "归属单位名称",
                    "attributeCode": "belong_unit_name",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "100",
                    "state": True,
                    "key": "6867deb98039403ca91c7cca94f87d8f"
                },
                {
                    "remark": "管理单位名称",
                    "createTime": "2025-02-07 16:53:41",
                    "createBy": "zzh",
                    "updateTime": "2025-02-07 16:53:41",
                    "updateBy": "zzh",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "管理单位名称",
                    "attributeCode": "manage_unit_name",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "100",
                    "state": True,
                    "key": "927cc5be0b3d4a2baef46e0883e4b8c2"
                },
                {
                    "remark": "分类名称",
                    "createTime": "2025-02-07 16:53:05",
                    "createBy": "zzh",
                    "updateTime": "2025-02-07 16:53:05",
                    "updateBy": "zzh",
                    "delFlag": False,
                    "tenantId": "defaultTenantId",
                    "createByName": None,
                    "updateByName": None,
                    "attributeName": "分类名称",
                    "attributeCode": "category_name",
                    "dataType": "String",
                    "attributeUnit": None,
                    "attributeType": None,
                    "attributeScope": "100",
                    "state": True,
                    "key": "e99b6edb835b45a19b1b749c35adb9ca"
                }
            ]
        }
        url='http://192.168.143.99:10092/idaas/idaas-gateway/talkweb-fusion-server/modelAttribute/saveAttribute'
        res=json.loads(base.send_post(url,param_type,headers=headers,data=data))['msg']
        print(f"{catalog_id[0]} 结果为：{res}")